const Koa=require('koa');
const router=require('koa-router')();
const app=new Koa();
const bodyParser=require('koa-bodyparser');

app.use(bodyParser())
app.use(async(ctx,next)=>{
	console.log(`Process ${ctx.request.method} ${ctx.request.url}`);
	await next();
})
router.get('/hello/:name',async (ctx,next)=>{
	var name=ctx.params.name;
	ctx.response.body=`<h1>Hello,${name}!</h1>`;
})
router.get('/',async(ctx,next)=>{
	ctx.response.body=`<h1>INDEX</h1>
	<form action='/singin' method='post'>
		<p>Name:<input name="name" value='koa'></p>
		<p>password:<input name='password' type="password"></p>
		<p><input type='submit' value="submit"></p>
	</form>`;
})
router.post('/singin',async(ctx,next)=>{
	var name=ctx.request.body.name||'',
	   password=ctx.request.body.password||'';
	   console.log(`singin with name:${name},password:${password}`);
	   if(name==='koa'&&password==='123456'){
	   	 ctx.response.body=`<h1>welcom,${name}</h1>`;
	   }else{
	   	 ctx.response.body=`<h1>Login failed</h1>
	   	 <p><a href="/">Try agin</a></p>`
	   	 
	   }
})
app.use(router.routes());
app.listen(3000);
console.log('app started at port 3000...');
